﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ivan_snake
{
    class GameEngine
    {
        private bool isStart{get;set;}
        private int gameSpeed{get;set;}
        private Snake snake;
        private Food food;
        private Field field;
        private GameRender GR = new GameRender();
        private GameController GC;
        public SnakeKey  PressedKey;

        public GameEngine()
        {
            this.isStart = false;
            this.gameSpeed = 0;
            this.snake = new Snake();
            this.food = new Food(snake);
            this.field = new Field(50, 50);
            this.GR = new GameRender();
            this.GC = new GameController();

            this.GC.KeyPressed += GC_KeyPressed;

        }

        private void GC_KeyPressed(object sender, SnakeKey e)
        {
              PressedKey  = e;

                switch (e)
                {
                    case SnakeKey.Right :
                        snake.MoveSnakeRight();
                        break;
                    case SnakeKey.Left :
                        snake.MoveSnakeLeft();
                        break;
                    case SnakeKey.Up:
                        snake.MoveSnakeUp();
                        break;
                    case SnakeKey.Down :
                        snake.MoveSnakeDown();
                        break;
                }
;
        }
        private void Move(SnakeKey e)
        {
              PressedKey  = e;

                switch (e)
                {
                    case SnakeKey.Right :
                        snake.MoveSnakeRight();
                        break;
                    case SnakeKey.Left :
                        snake.MoveSnakeLeft();
                        break;
                    case SnakeKey.Up:
                        snake.MoveSnakeUp();
                        break;
                    case SnakeKey.Down :
                        snake.MoveSnakeDown();
                        break;
                }
;
        }

        public void Start ()
        {
            isStart = true;
            while (isStart)
            {
               Thread.Sleep(500);
               GC.ReadKey();
               G_Tick();

                
              // snake.MoveSnakeLeft();
               // snake.MoveSnakeRight();
               // snake.MoveSnakeUp();
               //snake.MoveSnakeDown();

            }
              
        }

        void G_Tick()
        {
            Console.Clear();
            GR.RenderField(field);
            GR.RenderFood(food);
            GR.RenderSnake(snake);
            Move( PressedKey);

       
        }

        private void OnTick()
        {


            if (this.Tick != null)
            {
                this.Tick(this,null);
            }
        }
        public void Stop()
        {
            gameSpeed = 1000;
        }
        public void End()
        {
            isStart = false;
            gameSpeed = 0;
        }

        public event EventHandler Tick;

    }


    enum SnakeDirection
    {
        Left,
        Right,
        Up,
        Down
    }
}
